<template>
    <div class="con">
        <img src="../assets/images/logo.png" alt="" class="logo" onclick="return false">
        <img src="../assets/images/smlj.png" alt="" @click.prevent="scan()">                        
    </div>
</template>

<script>
export default {
    name: "index",
    data() {
        return {
            point_detail_id: ""
        };
    },
    beforeCreate() {
        document.getElementsByTagName("body")[0].className = "bg_MyPoints";
    },
    methods: {
        scan() {
            wx.scanQRCode({
                desc: "scanQRCode desc",
                needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
                success: res => {
                    var result = res.resultStr;
                    var arr = result.split("SCORE");
                    var qs = require("qs");
                    // 请求添加积分接口
                    this.axios({
                        method: "post",
                        url: `${this.siteUrl}/piaowu/api/point_add`,
                        data: qs.stringify({
                            member_id: $.cookie("osc_member_id"),
                            token: $.cookie("osc_member_token"),
                            point: $.trim(arr[1]),
                            code: result
                        })
                    })
                        .then(response => {
                    
                            if (response.data.status == 1) {
                                this.point_detail_id =
                                    response.data.data.point_detail_id;
                                this.$router.push({
                                    name: "giftDetail",
                                    params: {
                                        point: $.trim(arr[1]),
                                        point_detail_id: this.point_detail_id
                                    }
                                });
                            } else {
                                alert("请求失败：" + response.data.msg);
                            }
                        })
                        .catch(function(error) {
                            alert(error);
                        });
                },
                error: function(res) {
                    if (res.errMsg.indexOf("function_not_exist") > 0) {
                        alert("版本过低请升级");
                    }
                }
            });
        }
    },
    mounted: function() {
        var qs = require("qs");
        this.axios({
            method: "post",
            url: `${this.siteUrl}/piaowu/api/get_js_sign`,
            data: qs.stringify({
                url: location.host + location.pathname
            })
        })
            .then(response => {
                wx.config({
                    debug: false,
                    appId: response.data.data.appId,
                    timestamp: response.data.data.timestamp,
                    nonceStr: response.data.data.nonceStr,
                    signature: response.data.data.signature,
                    jsApiList: ["scanQRCode"]
                });
            })
            .catch(err => {
                console.log(err);
            });
    }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.con {
    text-align: center;
    position: relative;
}
.con .logo {
    width: 34%;
    position: absolute;
    left: 50%;
    transform: translate(-50%, 30%);
}

.con img {
    width: 100%;
}
</style>
